/****************************************************************************
 * Copyright 2009 Huascar A. Sanchez                                        *
 *                                                                          *
 * Licensed under the Apache License, Version 2.0 (the "License");          *
 * you may not use this file except in compliance with the License.         *
 * You may obtain a copy of the License at                                  *
 *                                                                          *
 *     http://www.apache.org/licenses/LICENSE-2.0                           *
 *                                                                          *
 * Unless required by applicable law or agreed to in writing, software      *
 * distributed under the License is distributed on an "AS IS" BASIS,        *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
 * See the License for the specific language governing permissions and      *
 * limitations under the License.                                           *
 ****************************************************************************/
package com.gotobject.filter.builders;

import com.gotobject.filter.Filter;
import com.gotobject.filter.spi.Xml;
import com.gotobject.filter.spi.ElementIterable;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;

import java.util.List;
import java.util.ArrayList;

/**
 * @author huascar.sanchez@gmail.com (Huascar A. Sanchez)
 */
public class XmlFilteringBuilder {
    private final Xml       xmlSource;
    private final Filter    filter;

    /**
     * construct an {@code XmlFilteringBuilder} object.
     * @param xmlSource
     *      an {@link com.gotobject.filter.spi.Xml}
     * @param filter
     *      a {@link Filter}
     */
    public XmlFilteringBuilder(Xml xmlSource, Filter filter) {
        this.xmlSource = xmlSource;
        this.filter    = filter;
    }
    
    private static <T extends Node> Iterable<T> asIterable(NodeList nodeList){
        return new ElementIterable<T>(nodeList);
    }


    public FilteringBuilder<Element> elements(String tag){
        final NodeList required = getElementsByTag(tag);
        return filter.from(XmlFilteringBuilder.<Element>asIterable(required));
    }

    public FilteringBuilder<Element> descendants(String tag) {
        final List<Element> elements = getDescendants(tag);
        return filter.from(elements);
    }


    public FilteringBuilder<Element> ancestors(String tag){
        final Iterable<Element> descendants = elements(tag).select();
        final List<Element>     ancestors   = new ArrayList<Element>();

        for(Element each : descendants){
            ancestors.add((Element) each.getParentNode());
        }

        return filter.from(ancestors);
    }

    private NodeList getElementsByTag(String tag) {
        final Document rawDoc  = xmlSource.currentDocument();
        return rawDoc.getElementsByTagName(tag);
    }

    private List<Element> getDescendants(String tag){
        final Document rawDoc  = xmlSource.currentDocument();
        final List<Element> elements = new ArrayList<Element>();
        for(Node each : asIterable(rawDoc.getElementsByTagName(tag))){
            for(Node eachChild : asIterable(each.getChildNodes())){
                if(eachChild instanceof Element) {
                    elements.add((Element) eachChild);
                }
            }
        }

        return elements;
    }


}
